package com.fz.song.controller.client;

import com.fz.song.exception.CustomException;
import com.fz.song.pojo.BlogBanner;
import com.fz.song.pojo.BlogInfo;
import com.fz.song.pojo.BlogType;
import com.fz.song.service.IBlogBannerService;
import com.fz.song.service.IBlogInfoService;
import com.fz.song.service.IBlogTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by sfz on 2018/5/23.
 * 主页controller
 */
@Controller
public class MainController {


    @Autowired
    private IBlogBannerService blogBannerService;
    @Autowired
    private IBlogInfoService blogInfoService;
    @Autowired
    private IBlogTypeService blogTypeService;


    @RequestMapping("/")
    public String index() {
        return "redirect:/index.html";
    }

    @RequestMapping("/index.html")
    public String indexHtml(HttpServletRequest request) {
        //banner
        List<BlogBanner> list = blogBannerService.getBannerList();
        request.setAttribute("bannerlist", list);
        //获取分类
        List<BlogType> typelist = blogTypeService.getBlogTypeList();
        request.setAttribute("typelist", typelist);
        //获取时间
        List<String> datelist = getdate();
        request.setAttribute("datelist", datelist);
        return "client/index";
    }

    @RequestMapping("blog_info_index_list_data")
    public String blog_info_index_list_data(HttpServletRequest request, Integer page,
                                            @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
                                            String typeid, String releasetime) throws CustomException {

        HashMap<String, String> param = new HashMap<>();
        param.put("typeid", typeid);
        param.put("releasetime", releasetime);
        //获取数据
        PageInfo<BlogInfo> pageInfo = blogInfoService.getBlogInfoIndexListData(page, limit, param);
        request.setAttribute("pageInfo", pageInfo);
        return "client/index_list_data";


    }


    @RequestMapping("/show_blog.html")
    public String show_blog_html(HttpServletRequest request, String id) throws CustomException {
        BlogInfo blogInfo = blogInfoService.getBlogInfoByid(id);
        if (blogInfo == null) {
            throw new CustomException("未找到对应的博客数据");
        }
        Long clicks = blogInfoService.getBlogInfoClicksByid(blogInfo);
        String typeid = blogInfo.getTypeid();
        BlogType blogType = blogTypeService.getBlogTypeByid(typeid);
        request.setAttribute("blogInfo", blogInfo);
        request.setAttribute("blogType", blogType);
        request.setAttribute("clicks", clicks);
        //查询数据
        return "client/show_blog";
    }


    public List<String> getdate() {
        ArrayList<String> strings = new ArrayList<>();
        SimpleDateFormat yrarsdf = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthsdf = new SimpleDateFormat("MM");
        String yrarstr = yrarsdf.format(new Date());
        String monthsdfstr = monthsdf.format(new Date());
        int yrar_int = Integer.parseInt(yrarstr);
        int monthsdf_int = Integer.parseInt(monthsdfstr);
        for (int j = 1; j <= monthsdf_int; j++) {
            strings.add(yrar_int + "-" + (j < 10 ? "0" + j : j));
        }
        return strings;
    }
}
